package cn.parus.controller;

import cn.parus.common.Execption.CustomException;
import cn.parus.common.Execption.Result;
import cn.parus.entity.FileShareLink;
import cn.parus.entity.FileSharing;
import cn.parus.service.FileReceviceService;
import cn.parus.service.FileShareLinkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Axel
 * @Description: TODO
 **/
@RestController
@RequestMapping("/share-file")
public class ShareFileController {

    @Autowired
    private FileShareLinkService fileShareLinkService;

    @Autowired
    private FileReceviceService fileReceviceService;

    /**
     * 查询文件分享链接
     * @param ownerId
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/search-all-link")
    public Result searchAllFileShare(
            @RequestParam(value = "ownerId", required = true) String ownerId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<FileShareLink> fileShareLink = fileShareLinkService.searchAllFileShare(ownerId, current, size);
            return Result.success(fileShareLink);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询我收到的文件
     * @param ownerId
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/search-all-receiver")
    public Result searchAllFileReceiver(
            @RequestParam(value = "ownerId", required = true) String ownerId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<FileSharing> fileSharing = fileReceviceService.searchAllFileRecevier(ownerId, current, size);
            return Result.success(fileSharing);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建共享链接
     * 需要具体的返回值，因为要具体的链接和提取码
     *
     * @param fileShareLink
     * @return
     */
    @PostMapping("/create-new-file-share-link")
    public Result createNewFileShareLink(@RequestBody FileShareLink fileShareLink) {
        try {
            FileShareLink res = fileShareLinkService.createNewShareFile(fileShareLink);
            return Result.success(res);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除共享链接
     *
     * @param fileShareLinkId
     * @return
     */
    @DeleteMapping("/delete-file-share-link")
    public Result deleteFileShareLink(@RequestParam(required = true) String fileShareLinkId) {
        try {
            fileShareLinkService.deleteFileShareLink(fileShareLinkId);
            return Result.success("删除成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除我收到的链接
     * @param shareId
     * @return
     */
    @DeleteMapping("/delete-file-share-receiver")
    public Result deleteFileShareReceiver(@RequestParam(required = true) String shareId) {
        try {
            fileReceviceService.deleteFileShareReceiver(shareId);
            return Result.success("删除成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }
}
